package com.qf.sportsevent.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.qf.sportsevent.entity.TitleChannelEntity;
import com.qf.sportsevent.fragment.NewsChildFragment;
import com.qf.sportsevent.util.UrlManager;

import java.util.List;

/**
 * Created by Administrator on 2016/12/15.
 */

public class CommonFragmentPagerAdapter<D> extends FragmentPagerAdapter {
    private List<D> list;

    public CommonFragmentPagerAdapter(FragmentManager fm, List<D> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        D d = list.get(position);
        if(d instanceof TitleChannelEntity) {
            String url = UrlManager.getNewsUrlById(((TitleChannelEntity) d).getId());
            return NewsChildFragment.newInstance(url);
        }else{
            return null;
        }
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        D d = list.get(position);
        if(d instanceof TitleChannelEntity) {
            return ((TitleChannelEntity) d).getName();
        }else{
            return null;
        }
    }

    public void updateList(List<D> list){
        this.list=list;
        notifyDataSetChanged();
    }
}
